package org.monk.phishphans2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class PhishPhans2Parser {

	public List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
	public Map<String, String> group;
	public String location;

	private String jString;
	private JSONArray jArray;
	public JSONObject jObject;

	public PhishPhans2Parser(String string) {
		jString = string;
	}

	/**
	 * @return
	 */
	public List<Map<String, String>> parseJsonForRecentShowList() {
		if (jString != null) {
			try {
				jArray = new JSONArray(jString);

				for (int i = 0; i < jArray.length(); i++) {
					jObject = jArray.getJSONObject(i);

					group = new HashMap<String, String>();
					group.put("showdate", jObject.getString("showdate"));
					group.put("nicedate", jObject.getString("nicedate"));
					group.put("venue", jObject.getString("venue"));
					groupData.add(group);
				}
			} catch (JSONException e) {
				Log.e("DEBUG: ", "JSON Parse error!");
			}
		} else {
			Log.e("ERROR:", "jString = null");
		}
		return groupData;
	}

	/**
	 * @return
	 */
	public List<Map<String, String>> parseJsonForYearsList() {
		if (jString != null) {
			try {
				jArray = new JSONArray(jString);

				for (int i = 0; i < jArray.length(); i++) {
					jObject = jArray.getJSONObject(i);

					group = new HashMap<String, String>();
					group.put("showdate", jObject.getString("showdate"));
					group.put("venue", jObject.getString("venue"));

					if (!jObject.getString("state").equals("")) {
						location = jObject.getString("city") + ", "
								+ jObject.getString("state") + ", "
								+ jObject.getString("country");
					} else {
						location = jObject.getString("city") + ", "
								+ jObject.getString("country");
					}

					group.put("location", location);
					groupData.add(group);
				}
			} catch (JSONException e) {
				Log.e("DEBUG: ", "JSON Parse error!");
			}
		} else {
			Log.e("ERROR:", "JSON years jString = null");
		}
		return groupData;
	}

	private String setlistData;
	private int setlistDataLength;

	public String notesString;
	public String newNotesString = "";
	public String encoresString;
	public String setsString;

	private int notesIndex;
	private int encoresIndex;

	public String[] setsArray;
	public String[] encoresArray;
	public String[] notesArray;

	// private JSONObject jObject;
	private JSONObject jObj;
	private JSONObject jObjSets;
	private JSONObject jObjEncores;
	private JSONObject jObjNotes;
	private JSONObject jObjSLNotes;

	public void seperateSetlistData(String setlistdata) {

		setlistData = nohtml(setlistdata);

		setlistDataLength = setlistData.length();

		if (setlistData.contains("[1]")) {
			notesIndex = setlistData.lastIndexOf("[1]");
			notesString = setlistData.substring(notesIndex + 3,
					setlistDataLength);
		} else {
			notesString = "";
		}

		if (setlistData.contains("Encore")) {
			encoresIndex = setlistData.indexOf("Encore:");

			if (setlistData.contains("[1]")) {
				notesIndex = setlistData.lastIndexOf("[1]");
				encoresString = setlistData.substring(encoresIndex, notesIndex);
			} else {
				encoresString = setlistData.substring(encoresIndex,
						setlistDataLength);
			}

		} else if (setlistData.contains("[1]")) {
			encoresIndex = setlistData.lastIndexOf("[1]");
		} else {
			encoresIndex = setlistDataLength;
		}

		setsString = setlistData.substring(0, encoresIndex);

		complete();
	}

	public void complete() {

		if (setsString != null)
			this.seperateSets();

		if (encoresString != null)
			this.seperateEncores();

		if (notesString != null && notesString != "")
			this.seperateNotes();

	}

	public void seperateSets() {
		setsArray = setsString.split("Set..:.");
	}

	public void seperateEncores() {
		encoresArray = encoresString.split("Encore:");
	}

	public void seperateNotes() {
		notesArray = notesString.split("\\[.\\]");
	}

	public String pVenue_text;
	public String pLocation_text;
	public String pDate_text;

	public String pSet1_text;
	public String pSet2_text;
	public String pSet3_text;
	public String pEncore1_text;
	public String pEncore2_text;
	public String pNotes_text;
	public String pSLNotes_text;

	public void parseJsonForSetlistData() throws JSONException {
		if (jString != null) {
			JSONArray jArr = new JSONArray(jString);
			JSONObject jOjt = jArr.getJSONObject(0);

			pVenue_text = jOjt.getString("venue");

			// check for international shows
			if (jOjt.getString("state").equals("")) {
				pLocation_text = jOjt.getString("city") + ", "
						+ jOjt.getString("country");
			} else {
				pLocation_text = jOjt.getString("city") + ", "
						+ jOjt.getString("state") + ", "
						+ jOjt.getString("country");
			}

			pDate_text = jOjt.getString("nicedate");
			String setlistdata = jOjt.getString("setlistdata");
			String setlistnotes = jOjt.getString("setlistnotes");

			if (setlistdata != null) {
				seperateSetlistData(setlistdata);
			} else {
				Log.e("ERROR:", "setlistdata = null");
			}

			switch (setsArray.length) {
			case 0:
				pSet1_text = "Set list data could not be loaded. Please contact the developer if this is an error.";
				pSet2_text = "EMPTY";
				pSet3_text = "EMPTY";
				break;
			case 1:
				pSet1_text = "Set list data could not be loaded. Please contact the developer if this is an error.";
				pSet2_text = "EMPTY";
				pSet3_text = "EMPTY";
				break;
			case 2:
				pSet1_text = "Set 1: " + setsArray[1];
				pSet2_text = "EMPTY";
				pSet3_text = "EMPTY";
				break;
			case 3:
				pSet1_text = "Set 1: " + setsArray[1];
				pSet2_text = "Set 2: " + setsArray[2];
				pSet3_text = "EMPTY";
				break;
			case 4:
				pSet1_text = "Set 1: " + setsArray[1];
				pSet2_text = "Set 2: " + setsArray[2];
				pSet3_text = "Set 3: " + setsArray[3];
				break;
			}
			// Log.e("ERROR:",Arrays.toString(encoresArray));
			if (encoresArray != null) {
				switch (encoresArray.length) {
				case 0:
					pEncore1_text = "EMPTY";
					pEncore2_text = "EMPTY";
					break;
				case 1:
					pEncore1_text = "EMPTY";
					pEncore2_text = "EMPTY";
					break;
				case 2:
					pEncore1_text = "Encore 1: " + encoresArray[1];
					pEncore2_text = "EMPTY";
					break;
				case 3:
					pEncore1_text = "Encore: " + encoresArray[1];
					pEncore2_text = "Encore 2: " + encoresArray[2];
					break;
				}
			}

			if (notesArray != null) {
				for (int i = 0; i < notesArray.length; i++) {
					int number = i + 1;
					newNotesString += "[" + number + "] " + notesArray[i]
							+ "\n";
				}
			} else {
				newNotesString = "";
			}

			pNotes_text = newNotesString;
			pSLNotes_text = setlistnotes;
		} else {
			// TODO SetList jString=null
		}
	}

	public JSONObject convertSetlistDataToJSON() throws JSONException {

		jObj = new JSONObject();

		jObjSets = new JSONObject();
		if (setsArray != null) {
			for (int j = 1; j < setsArray.length; j++) {
				jObjSets.put("Set " + j, setsArray[j]);
			}
		}
		jObj.put("Sets", jObjSets);

		jObjEncores = new JSONObject();
		if (encoresArray != null) {
			for (int j = 1; j < encoresArray.length; j++) {
				jObjEncores.put("Encore " + j, encoresArray[j]);
			}
		}
		jObj.put("Encores", jObjEncores);

		jObjNotes = new JSONObject();
		if (notesArray != null) {
			for (int j = 0; j < notesArray.length; j++) {
				int footNote = j + 1;
				jObjNotes.put("Note " + footNote, notesArray[j]);
			}
		}
		jObj.put("Notes", jObjNotes);
		jObj.put("SLNotes", jObjSLNotes);

		// return jObj.toString();
		return jObj;
	}

	public List<Map<String, String>> parseJsonForReviews() {
		if (jString != null) {
			try {
				jArray = new JSONArray(jString);

				for (int i = 0; i < jArray.length(); i++) {
					jObject = jArray.getJSONObject(i);

					group = new HashMap<String, String>();
					group.put("author", jObject.getString("author"));
					group.put("review", jObject.getString("review"));
					groupData.add(group);
				}
			} catch (JSONException e) {
				Log.e("DEBUG: ", "JSON Reviews parse error!");
			}
		}
		return groupData;
	}

	public String nohtml(String json) {
		return json.toString().replaceAll("\\<.*?>", "");
	}

	public String parseJsonForAuthorize() {
		if (jString != null) {
			String authkey;
			try {
				jArray = new JSONArray(jString);

				for (int i = 0; i < jArray.length(); i++) {
					jObject = jArray.getJSONObject(i);

					if (jObject.getString("success") == "1") {
						authkey = jObject.getString("authkey");
						return authkey;
					} else {
						return "0";
					}

				}
			} catch (JSONException e) {
				Log.e("DEBUG: ", "JSON Authorize parse error!");
			}

		}

		return "0";
	}

}
